package com.uzykj.modules.front.service;

import com.uzykj.modules.front.entity.FollowUser;
import com.uzykj.modules.front.mapper.FollowUserMapper;
import com.uzykj.modules.sys.entity.User;
import com.uzykj.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ghostxbh
 * @date 2020/8/18
 * @description
 */
@Service
public class FollowUserService {
    @Autowired
    private FollowUserMapper followUserMapper;

    public List<User> followers(String id) {
        List<FollowUser> fanses = followUserMapper.getFanses(new FollowUser(id, 1));
        return Optional.ofNullable(fanses)
                .orElse(new ArrayList<FollowUser>())
                .stream()
                .map(fans -> UserUtils.get(fans.getFollowId()))
                .collect(Collectors.toList());
    }

    public List<User> fanses(String id) {
        List<FollowUser> fanses = followUserMapper.getFanses(new FollowUser(id, 2));
        return Optional.ofNullable(fanses)
                .orElse(new ArrayList<FollowUser>())
                .stream()
                .map(fans -> UserUtils.get(fans.getFollowedId()))
                .collect(Collectors.toList());
    }

    public void add(String follow, String followed) {
        FollowUser f = new FollowUser();
        f.setFollowId(follow);
        f.setFollowedId(followed);
        f.setCreateDate(new Date());
        followUserMapper.insert(f);
    }

    public void del(String follow, String followed) {
        FollowUser f = new FollowUser();
        f.setFollowId(follow);
        f.setFollowedId(followed);
        followUserMapper.delete(f);
    }
}
